<template>
  <!-- ElementUI布局 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>个人健康平台</span>
      </div>
    </el-header>
    <!-- 主题 -->
    <el-container>
      <!-- 左导航 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapase()">|||</div>
        <!-- unique-opened：只打开一个一级菜单 -->
        <!-- default-active：默认选中 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePaht"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="main.id + ''" v-for="main in menuList" :key="main.id">
            <template slot="title">
              <i :class="main.icons"></i>
              <span>{{ main.title }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="sub.path"
              v-for="sub in main.subList"
              :key="sub.id"
              @click="saveNavState(sub.path)"
            >
              <template slot="title">
                <i :class="sub.icons"></i>
                <span>{{ sub.title }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: true,
      activePaht: "/welcome", // 默认路径
    };
  },
  // 页面记载事件
  created() {
    // 查询Menu List
    this.getMenuList();
    // 获取之前点击的路径
    this.activePaht = window.sessionStorage.getItem("activePath");
  },
  methods: {
    async getMenuList() {
      //获取导航
      const { data: res } = await this.$http.post("/menus");
      if (res.flag == "OK") {
        this.menuList = JSON.parse(res.data);
      } else {
        this.$message.error("请检查数据库！");
      }
    },
    // 控制伸缩
    toggleCollapase() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存路径
    saveNavState(path) {
      window.sessionStorage.setItem("activePath",path);// 存放点击的路径
      this.activePaht = path;
    },
  },
};
</script>

<style lang="less" scoped>
/* 布局填充页面 */
.home-container {
  height: 100%;
}
/* 布局样式 */
.el-header {
  background-color: #373d41;
  /* 元素左右贴边 */
  display: flex;
  justify-content: space-between;
  padding-left: 0%; /* 左边界为0 */
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      widows: 55px;
      height: 55px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  // ||| 伸缩按钮
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #ffffff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
